﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using System.Xml;
using System.Text;

public partial class TaoBaoApi_TB_BaoBei : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private string Key
    {
        get
        {
            if (Request.QueryString["key"] == null || Request.QueryString["key"].ToString().Length == 0)
                return null;
            return Request.QueryString["key"].ToString();
        }
    }
    private int ViewIndex
    {
        get
        {
            if (Request.QueryString["index"] == null || Request.QueryString["index"].ToString().Length == 0)
                return 0;
            return Convert.ToInt32(Request.QueryString["index"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Key == null)
            {
                AspNetPager1.PageSize = ConfigurationManager.AppSettings["RowCountPerPage"].ToString().Length > 0 ?
                    Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString()) : 10;
                SearchControl1.InitParam("TB_BaoBei", "MID", "BBMC", "BaoBei1", "rptTB_BAOBEI", "AspNetPager1");
                Bind();
                div_info.Visible = false;
            }
            else
            {
                BindOne();
                div_info.Visible = true;
                BindSPList();
                if (ViewIndex != 0)
                    MultiView1.ActiveViewIndex = ViewIndex;
            }
        }
    }
    void Bind()
    {
        int pageCount = 0;
        int rowCount = 0;
        List<Model.TaoBao.TB_BAOBEI> properties = new BLL.TaoBao.TB_BAOBEI().GetList(this.AspNetPager1.PageSize,
            this.AspNetPager1.CurrentPageIndex, "MID", false, "", out pageCount, out rowCount);
        BaoBei1.Bind(properties);
        this.AspNetPager1.RecordCount = rowCount;        
    }
    void BindOne()
    {        
        List<Model.TaoBao.TB_BAOBEI> ps = new List<Model.TaoBao.TB_BAOBEI>();
        int MID=0;
        try{
            MID=Convert.ToInt32(Key);
        }
        catch
        {
            Response.Redirect("tb_baobei.aspx");
            return;
        }
        Model.TaoBao.TB_BAOBEI p = new BLL.TaoBao.TB_BAOBEI().GetModel(MID);
        ps.Add(p);
        BaoBei1.Bind(ps);
        this.AspNetPager1.RecordCount = 1;
        txtLocked.Text = p.field2;

        //淘宝属性
        List<Model.TaoBao.TB_BaobeiProps> baobeiProps = GetBBPropsbyPid(MID);
        rItemProps.DataSource = baobeiProps;
        rItemProps.DataBind();
    }
    // 查询mid商品的全部propsvalue
    List<Model.TaoBao.TB_BaobeiProps> GetBBPropsbyPid(int pid)
    {
        List<Model.TaoBao.TB_BaobeiProps> propList = new BLL.TaoBao.TB_BaobeiProps().GetAllList();
        List<Model.TaoBao.TB_BaobeiProps> props = new List<Model.TaoBao.TB_BaobeiProps>();
        foreach (Model.TaoBao.TB_BaobeiProps p in propList)
            if (p.PID == pid)
                props.Add(p);
        return props;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        int pageCount = 0;
        int rowCount = 0;
        if (SearchControl1.IsSearchResult)
        {
            Repeater rpt = (Repeater)BaoBei1.FindControl("rptTB_BAOBEI");
            if (rpt != null)
            {
                string strSql;
                SearchControl1.BindParentPageControl(rpt, AspNetPager1, SearchControl1.DataTableName, SearchControl1.PrimaryKeyColumnName,
                    SearchControl1.StrCondition, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out rowCount, out strSql);
            }
        }
        else
        {
            List<Model.TaoBao.TB_BAOBEI> colors = new BLL.TaoBao.TB_BAOBEI().GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex,
                "MID", false, "", out pageCount, out rowCount);
            BaoBei1.Bind(colors);
        }
    }
   
    /// <summary>
    /// 显示提示信息
    /// </summary>
    void ShowMessage(string msg)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + msg + "')", true);
    }
    /// <summary>
    /// 下载淘宝宝贝
    /// </summary>   
    protected void btnDown_Click(object sender, EventArgs e)
    {
        Response.Redirect("tb_downbaobei.aspx");
    }
    /// <summary>
    /// 显示多个Tab
    /// </summary>
    protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
    {
        MultiView1.ActiveViewIndex = Convert.ToInt32(Menu1.SelectedValue);
    }
    /// <summary>
    /// 绑定商品大类
    /// </summary>
    void BindMainType()
    {
        
    }
    /// <summary>
    /// 绑定商品子类
    /// </summary>
    void BindPraType()
    {
    }
    /// <summary>
    /// 绑定本地库存商品列表
    /// </summary>
    void BindSPList()
    {
        string sql = "select SPDM,SPMC from shangpin order by SPDM";
        DataSet ds= new BLL.SHANGPIN().GetListBySql(sql);
        ddlSPDM.DataSource = ds;
        ddlSPDM.DataValueField = "SPDM";
        ddlSPDM.DataTextField = "SPMC";
        ddlSPDM.DataBind();
        if (txtLocked.Text.Length > 0)
            ddlSPDM.SelectedValue = txtLocked.Text;
    }    
    /// <summary>
    /// 关联淘宝宝贝和本地商品
    /// </summary>   
    protected void btnLock_Click(object sender, EventArgs e)
    {
        Model.TaoBao.TB_BAOBEI baobei = new BLL.TaoBao.TB_BAOBEI().GetModel(Convert.ToInt32(Key));
        baobei.field2 = ddlSPDM.SelectedValue;
        new BLL.TaoBao.TB_BAOBEI().Update(baobei);
        Response.Redirect("tb_baobei.aspx?key="+Key+"&index=8");
    }
}
